package scrapygui;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

import java.util.List;

public class Main extends Application {
    private ScreenCopyModel mSCModel;

    public static void main(String[] args) { launch( args ); }

    @Override
    public void start(Stage primaryStage) {
        mSCModel = new ScreenCopyModel();
        primaryStage.setTitle("Screen Copy GUI");
        refreshUI( primaryStage );
    }


    private void refreshUI(Stage primaryStage) {
        int width = 280;
        int height = 170;
        GridPane gp = new GridPane();
        gp.setHgap( 20 );
        gp.setVgap( 20 );
        gp.setAlignment( Pos.CENTER );
        List<DeviceBean> list = mSCModel.queryDevices( ViewUtils::showDialog );
        //刷新按钮
        gp.add( new Label( "Refresh device" ), 0, 0 );
        gp.add( ViewUtils.createRefreshButton( event -> refreshUI( primaryStage )), 1, 0 );

        //启动全部设备
        gp.add( new Label( "Start all device" ), 0, 1 );
        gp.add(ViewUtils.createStartAllButton(
                list, via -> mSCModel.startScreen( via, ViewUtils::showDialog )
        ), 1, 1);

        for (int i = 0; i < list.size(); i++) {
            DeviceBean bean = list.get( i );
            if( bean == null ) continue;
            gp.add( new Label( "Device " + ( i + 1 ) ), 0, i + 2 );
            gp.add( ViewUtils.createDeviceButton(
                    bean,
                    via -> mSCModel.startScreen( via, ViewUtils::showDialog ) ), 1, i + 2
            );
        }
        primaryStage.setScene( new Scene( gp, width, height ) );
        primaryStage.setMaxWidth( width );
        primaryStage.setMinHeight( height );
        primaryStage.show();
    }

}
